#include "../lvgl/examples/lv_examples.h"
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include <string.h>


static P_Node_t FileList ;
lv_obj_t * obj;
static P_Node_t Tmp ;
static lv_obj_t * img ;
static lv_obj_t * gif ;
bool gifFlag ;

void BtnTouch (lv_event_t * e) 
{
	// 获取用户点解的按键是哪一个
	lv_obj_t * label = lv_event_get_user_data(e);

	char * msg = lv_label_get_text( label );

	char NewPath[256]={0} ;

	if(msg[0] == 'P')
	{
		printf("上一张..\n");
		Tmp = Tmp->Prev ;
		if (Tmp == FileList)
		{
			Tmp = Tmp->Prev ;
		}
	}
	else{
		Tmp = Tmp->Next ;
		if (Tmp == FileList)
		{
			Tmp = Tmp->Next ;
		}
		printf("下一张..\n");
	}

	snprintf( NewPath , sizeof(NewPath) , "A:%s" ,  Tmp->Data.FilePath );
	printf("NewPaht:%s\n" , NewPath );

	// if (Tmp->Data.Type == 'g')
	// {
	// 	/* GIF处理 */
		
	// 	if (gif == NULL)
	// 	{
	// 		gif = lv_gif_create( obj );
	// 		lv_obj_set_align( gif , LV_ALIGN_TOP_MID );
	// 	}
	// 	lv_gif_set_src( gif , NewPath );
	// }
	// else{
	// 	/* 静态图 */
	// 	if (gif != NULL)
	// 	{
	// 		lv_obj_del(gif);
	// 		gif = NULL ;
	// 	}
	// 	lv_img_set_src( img , NewPath );
	// }



	if (Tmp->Data.Type == 'g') {
    /* GIF处理 */
    if (img != NULL) {
        lv_obj_del(img);
        img = NULL;
    }
    if (gif == NULL) {
        gif = lv_gif_create(obj);
        lv_obj_set_align(gif, LV_ALIGN_TOP_MID);
		gifFlag = true;
    }
    lv_gif_set_src(gif, NewPath);
	} else {
    /* 静态图 */
  	 if (gif != NULL && gifFlag  )
		{
			lv_obj_del(gif);
			gif = NULL ;
			gifFlag = false ;
		}
    if (img == NULL) {
        img = lv_img_create(obj);
        lv_obj_set_align(img, LV_ALIGN_TOP_MID);
    }
    lv_img_set_src(img, NewPath);
}
			
}


lv_style_t * FontStyleInit()
{

	/*Create a font*/
    static lv_ft_info_t info;
    /*FreeType uses C standard file system, so no driver letter is required.*/
    info.name = "/usr/share/fonts/simkai.ttf";
    info.weight = 28;
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    /*Create style with the new font*/
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, info.font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

	return &style ;
}



void InitUi()
{

	lv_style_t * fontStyle = FontStyleInit();

	//////////////////初始化图像显示区///////////////////////
	// 创建一个基础对象（矩形 ，作为图像显示区）
	obj = lv_obj_create( lv_scr_act() );
	lv_obj_set_size( obj , 780 , 400 );
	lv_obj_align(obj , LV_ALIGN_TOP_MID , 0 , 10 ) ;


	// 初始化样式
	static lv_style_t style;  // 定义一个新的样式类型
	lv_style_init(&style); // 初始化新的样式

	// 设置圆角、透明度、背景色、阴影等样式属性
	lv_style_set_radius(&style, 5);
	lv_style_set_bg_opa(&style, LV_OPA_COVER);
	lv_style_set_bg_color(&style, lv_palette_lighten(LV_PALETTE_GREY, 1));
	lv_style_set_shadow_width(&style, 55);
	lv_style_set_shadow_color(&style, lv_palette_main(LV_PALETTE_BLUE));

	// 将样式添加到obj
	lv_obj_add_style(obj, &style, 0);


	//////////////////初始化按钮///////////////////////
	lv_obj_t * btnPrev = lv_btn_create(lv_scr_act());
	lv_obj_set_size( btnPrev , 130  , 60 );
	lv_obj_align(btnPrev , LV_ALIGN_BOTTOM_MID , -200 , -10 ) ;

	lv_obj_t * labelPrev = lv_label_create( btnPrev );
	lv_obj_add_style( labelPrev , fontStyle , 0 );
	lv_label_set_text(labelPrev , "P上一张") ;


	lv_obj_t * btnNext = lv_btn_create(lv_scr_act());
	lv_obj_set_size( btnNext , 130 , 60 );
	lv_obj_align(btnNext , LV_ALIGN_BOTTOM_MID , 200 , -10 ) ;

	lv_obj_t * labelNext = lv_label_create( btnNext );
	lv_obj_add_style( labelNext , fontStyle , 0 );
	lv_label_set_text(labelNext , "N下一张") ;

	// 注册点击事件的响应函数
	lv_obj_add_event_cb(btnPrev , BtnTouch , LV_EVENT_CLICKED , labelPrev ) ;
	lv_obj_add_event_cb(btnNext , BtnTouch , LV_EVENT_CLICKED , labelNext ) ;

	// 创建静态图像对象
	img = lv_img_create( obj );
	gif = lv_gif_create( obj );

	


	lv_obj_set_align( gif , LV_ALIGN_TOP_MID );
	lv_obj_set_align( img , LV_ALIGN_TOP_MID );




}


void myMain( void )
{
	// 初始化链表
	FileList = InitNewNode( NULL );
	if (FileList == NULL)
	{
		perror("list Init Error");
	}

	// 让tmp指向第一个有效数据
	Tmp = FileList->Next ;


	// 目录检索
	int retVal = DirRetrieval( "/jakl/img/" , ".bmp" , FileList );
	printf("检索到%d个bmp文件..\n" , retVal);


	retVal = DirRetrieval( "/jakl/img/" , ".jpg" , FileList );
	printf("检索到%d个jpg文件..\n" , retVal);

	retVal = DirRetrieval( "/jakl/img/" , ".png" , FileList );
	printf("检索到%d个png文件..\n" , retVal);

	retVal = DirRetrieval( "/jakl/img/" , ".gif" , FileList );
	printf("检索到%d个gif文件..\n" , retVal);


	// 界面初始化
	InitUi();
	


}